﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor
{
    public class PythonScriptTreeItemPopBar : Popup
    {
        public PythonScriptTreeItemPopBar(PythonScriptTreeItem ownerItem)
        {
            this.ownerItem = ownerItem;
            this.Placement = PlacementMode.Mouse;

            StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, };

            btnNewFolder.Style = FindResource("LSquareButtonStyle") as Style;
            btnNewFolder.BorderThickness = new Thickness(1);
            btnNewFolder.Click += BtnNewFolder_Click;
            sp.Children.Add(btnNewFolder);

            btnNewScript.Style = FindResource("LSquareButtonStyle") as Style;
            btnNewScript.BorderThickness = new Thickness(1);
            btnNewScript.Click += BtnNewScript_Click;
            sp.Children.Add(btnNewScript);

            btnNewScriptModel.Style = FindResource("LSquareButtonStyle") as Style;
            btnNewScriptModel.BorderThickness = new Thickness(1);
            btnNewScriptModel.Click += BtnNewScriptModel_Click;
            sp.Children.Add(btnNewScriptModel);

            if (ownerItem.IsPythonScriptFile)
            {
                btnRunScript.Style = FindResource("LSquareButtonStyle") as Style;
                btnRunScript.BorderThickness = new Thickness(1);
                btnRunScript.Click += BtnRunScript_Click;
                sp.Children.Add(btnRunScript);

                btnEditScript.Style = FindResource("LSquareButtonStyle") as Style;
                btnEditScript.BorderThickness = new Thickness(1);
                btnEditScript.Click += BtnEditScript_Click;
                sp.Children.Add(btnEditScript);
            }

            btnDeleteScriptOrFolder.Style = FindResource("LSquareButtonStyle") as Style;
            btnDeleteScriptOrFolder.BorderThickness = new Thickness(1);
            btnDeleteScriptOrFolder.Click += BtnDeleteScriptOrFolder_Click;
            sp.Children.Add(btnDeleteScriptOrFolder);

            if (ownerItem.GroupName == "~LME_内置示例~")
            {
                btnCopyInnerGlobalScript.Style = FindResource("LSquareButtonStyle") as Style;
                btnCopyInnerGlobalScript.BorderThickness = new Thickness(1);
                btnCopyInnerGlobalScript.Click += BtnCopyInnerGlobalScript_Click;
                sp.Children.Add(btnCopyInnerGlobalScript);
            }

            btnLocate.Style = FindResource("LSquareButtonStyle") as Style;
            btnLocate.BorderThickness = new Thickness(1);
            btnLocate.Click += BtnLocate_Click;
            sp.Children.Add(btnLocate);

            mainBorder = new Border()
            {
                Child = sp,
                BorderBrush = Brushes.Black,
                Padding = new Thickness(1),
                SnapsToDevicePixels = true,
                BorderThickness = new Thickness(1),
                Background = Brushes.WhiteSmoke,
            };

            RenderOptions.SetBitmapScalingMode(mainBorder, BitmapScalingMode.NearestNeighbor);
            this.Child = mainBorder;
        }

        private void BtnLocate_Click(object sender, RoutedEventArgs e)
        {
            if (ownerItem == null) return;

            ownerItem.LocateInExplorer();
        }

        private void BtnCopyInnerGlobalScript_Click(object sender, RoutedEventArgs e)
        {
            if (ownerItem == null) return;

            ownerItem.DeleteScriptFileOrCopyInnerScirpt();
        }

        private void BtnDeleteScriptOrFolder_Click(object sender, RoutedEventArgs e)
        {
            if (ownerItem == null) return;

            switch (ownerItem.Location)
            {
                case PythonScriptLocation.Global:
                    Globals.MainWindow.DeleteGlobalScriptEntries();
                    break;
                case PythonScriptLocation.Workspace:
                    Globals.MainWindow.DeleteWorkspaceScriptEntries();
                    break;
            }
        }

        private void BtnRunScript_Click(object sender, RoutedEventArgs e)
        {
            if (ownerItem == null) return;

            if (ownerItem.IsPythonScriptFile == false)
            {
                LMessageBox.ShowWarning("此条目不是个脚本，不能运行！");
                return;
            }

            ownerItem.RunScript();
        }

        private void BtnNewScriptModel_Click(object sender, RoutedEventArgs e)
        {
            if (ownerItem == null) return;

            switch (ownerItem.Location)
            {
                case PythonScriptLocation.Global:
                    {
                        var newFolderItem = Globals.MainWindow.CreateScriptFolder(
                            Globals.MainWindow.tvGlobalScriptFolder);
                        if (newFolderItem != null)
                        {
                            Globals.MainWindow.CreatePythonScriptInFolder(newFolderItem, false,
                                MainWindow.ScriptType.ModelScript);
                        }
                        break;
                    }
                case PythonScriptLocation.Workspace:
                    {
                        var newFolderItem = Globals.MainWindow.CreateScriptFolder(
                            Globals.MainWindow.tvWorkspaceScriptFolder);
                        if (newFolderItem != null)
                        {
                            Globals.MainWindow.CreatePythonScriptInFolder(newFolderItem, false,
                                MainWindow.ScriptType.ModelScript);
                        }
                        break;
                    }
            }
        }

        private void BtnNewScript_Click(object sender, RoutedEventArgs e)
        {
            if (ownerItem == null) return;

            switch (ownerItem.Location)
            {
                case PythonScriptLocation.Global:
                    Globals.MainWindow.NewGlobalScript();
                    break;
                case PythonScriptLocation.Workspace:
                    Globals.MainWindow.NewWorkspaceScript();
                    break;
            }
        }

        private void BtnNewFolder_Click(object sender, RoutedEventArgs e)
        {
            if (ownerItem == null) return;

            switch (ownerItem.Location)
            {
                case PythonScriptLocation.Global:
                    Globals.MainWindow.CreateScriptFolder(Globals.MainWindow.tvGlobalScriptFolder);
                    break;
                case PythonScriptLocation.Workspace:
                    Globals.MainWindow.CreateScriptFolder(Globals.MainWindow.tvWorkspaceScriptFolder);
                    break;
            }
        }

        private void BtnEditScript_Click(object sender, RoutedEventArgs e)
        {
            if (ownerItem == null) return;

            ownerItem.EditScriptFile();
        }

        private Border mainBorder;

        public Border MainBorder { get { return this.mainBorder; } }

        private PythonScriptTreeItem ownerItem = null;

        public PythonScriptTreeItem OwnerItem
        {
            get { return this.ownerItem; }
        }

        private Button btnNewFolder = new Button()
        {
            Padding = new Thickness(1),
            Margin = new Thickness(1),
            Height = 24,
            Width = 24,
            Content = new Image()
            {
                Width = 16,
                Height = 16,
                Source = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/folder-new-2.png")),
            },
            ToolTip = "创建子目录",
        };
        public Button BtnNewFolder { get { return btnNewFolder; } }

        private Button btnNewScript = new Button()
        {
            Padding = new Thickness(1),
            Margin = new Thickness(1),
            Height = 24,
            Width = 24,
            Content = new Image()
            {
                Width = 16,
                Height = 16,
                Source = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/script-add.png")),
            },
            ToolTip = "创建脚本",
        };
        public Button BtnNewScript { get { return btnNewScript; } }

        private Button btnNewScriptModel = new Button()
        {
            Padding = new Thickness(1),
            Margin = new Thickness(1),
            Height = 24,
            Width = 24,
            Content = new Image()
            {
                Width = 16,
                Height = 16,
                Source = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/model.png")),
            },
            ToolTip = "创建模块",
        };
        public Button BtnNewScriptModel { get { return btnNewScriptModel; } }

        private Button btnRunScript = new Button()
        {
            Padding = new Thickness(1),
            Margin = new Thickness(1),
            Height = 24,
            Width = 24,
            Content = new Image()
            {
                Width = 24,
                Height = 24,
                Source = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/runit.png")),
            },
            ToolTip = "运行脚本",
        };
        public Button BtnRunScript { get { return btnRunScript; } }

        private Button btnEditScript = new Button()
        {
            Padding = new Thickness(1),
            Margin = new Thickness(1),
            Height = 24,
            Width = 24,
            Content = new Image()
            {
                Width = 24,
                Height = 24,
                Source = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/edit.png")),
            },
            ToolTip = "编辑脚本",
        };
        public Button BtnEditScript { get { return btnEditScript; } }

        private Button btnDeleteScriptOrFolder = new Button()
        {
            Padding = new Thickness(1),
            Margin = new Thickness(1),
            Height = 24,
            Width = 24,
            Content = new Image()
            {
                Width = 24,
                Height = 24,
                Source = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/delete.png")),
            },
            ToolTip = "删除",
        };
        public Button BtnDeleteScriptOrFolder { get { return btnDeleteScriptOrFolder; } }

        private Button btnCopyInnerGlobalScript = new Button()
        {
            Padding = new Thickness(1),
            Margin = new Thickness(1),
            Height = 24,
            Width = 24,
            Content = new Image()
            {
                Width = 24,
                Height = 24,
                Source = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/copy-from-lmescript.png")),
            },
            ToolTip = "从内置脚本生成新脚本",
        };
        public Button BtnCopyInnerGlobalScript { get { return btnCopyInnerGlobalScript; } }

        private Button btnLocate = new Button()
        {
            Padding = new Thickness(1),
            Margin = new Thickness(1),
            Height = 24,
            Width = 24,
            Content = new Image()
            {
                Width = 24,
                Height = 24,
                Source = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/target.png")),
            },
            ToolTip = "在资源管理器中定位",
        };
        public Button BtnLocate { get { return btnLocate; } }
    }
}
